//
//  KSFilter2Cell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/12/20.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit

class KSFilter2Cell: UITableViewCell {
    var btnClosure:((Int)->())?
    var numberOfRow = 0
    var titles:[SeString]? {
        didSet {
            if let titles = titles {
                if itemBtnArr.count == numberOfRow {
                    
                } else {
                    let padH = 10
                    let itemH = 30
                    let rowx = numberOfRow
                    let contentVWidth = KSGlobal.screenW
                    let itemW = (Int(contentVWidth) - padH * (rowx + 1)) / rowx
                    for i in 0..<itemBtnArr.count {
                        itemBtnArr[i].removeFromSuperview()
                    }
                    itemBtnArr.removeAll()
                    for i in 0..<numberOfRow {
                        let btn = SlideItemButton()
                        
                        btn.addTouchUpInSideAction { [i] bbb in
                            if let clo = self.btnClosure {
                                clo(i)
                             }
//                            self.titles![i].ks_selected = !self.titles![i].ks_selected
//                            (bbb as! SlideItemButton).setSelected(self.titles![i].ks_selected)
                        }
                        itemBtnArr.append(btn)
                        self.contentView.addSubview(btn)
                        btn.snp.makeConstraints { (make) in
                            make.width.equalTo(itemW)
                            make.height.equalTo(itemH)
                            make.left.equalTo(self.contentView).offset(padH + i%rowx * (itemW + padH))
                            make.top.equalTo(self.contentView).offset(5)
                            make.bottom.equalTo(self.contentView).offset(-1)
                        }
                    }
                }
                for i in 0..<itemBtnArr.count {
                    if i < titles.count {
                        itemBtnArr[i].isHidden = false
                        itemBtnArr[i].setTitle(titles[i].title,for: .normal)
                        itemBtnArr[i].setSelected(titles[i].ks_selected)
                    } else {
                        itemBtnArr[i].isHidden = true
                    }
 
                }
            }
        }
    }
    var itemBtnArr = [SlideItemButton]()
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        
    }
    
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
